package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.managerDao;
import com.entity.manager;

public class Check_UserServlet extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		//处理字符编码
		
		//处理请求
		String action=request.getParameter("action");
		if(action.equals("checkuser")){
			String user=request.getParameter("loginuser");
			managerDao man=new managerDao();
			boolean bn=man.select(user);
			String mess=null;
			if(bn){
				mess="账号输入正确!";
			}else{
				mess="账号输入错误!";
			}
			PrintWriter pw=response.getWriter();
			pw.write(mess);
		}else if(action.equals("login")){
			String user=request.getParameter("user");
			String pwd=request.getParameter("pwd");
			//处理数据库
			managerDao man=new managerDao();
			boolean bn=man.select(user, pwd);
			//相应 页面跳转
			if(!bn){
				request.setAttribute("mess", "账号或密码错误!");
				request.getRequestDispatcher("login.jsp").forward(request, response);
			}else{
				manager ma=new manager(user, pwd);
				request.getSession().setAttribute("ma", ma);
				response.sendRedirect("index.jsp");
			}
		}else if(action.equals("yzm")){
			String yzm=request.getParameter("yzm");
			String yzm2=(String) request.getSession().getAttribute("validate");
			if(yzm.equalsIgnoreCase(yzm2)){
				response.getWriter().write("√");
			}else{
				response.getWriter().write("×");
			}
		}
		
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
